package com.manager.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manager.mapper.TbCommodityMapper;
import com.manager.mapper.TbOutlibraryMapper;
import com.manager.pojo.TbInlibrary;
import com.manager.pojo.TbInlibraryExample;
import com.manager.pojo.TbOutlibrary;
import com.manager.pojo.TbOutlibraryExample;
import com.manager.pojo.TbOutlibraryExample.Criteria;
import com.manager.service.OutLibrarService;
import com.manager.vo.TbUserEx;

@Service
public class OutLibrarServiceimpl implements OutLibrarService {

	@Autowired
	private TbOutlibraryMapper tbOutlibraryMapper;
	
	@Autowired
	private TbCommodityMapper tbCommodityMapper;
	
	@Override		//获取全部出库分页信息
	public TbUserEx getOutLibraryList(Integer page, Integer rows) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbOutlibraryExample example = new TbOutlibraryExample();
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andOutlibraryNotEqualTo(0);
		//分页范围设置
		PageHelper.startPage(page,rows);
		
		List<TbOutlibrary>list = null;
		
		try {				//查询
			list = tbOutlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("出库列表获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			
			tbUserEx.setCode(500);
			tbUserEx.setError("出库列表没有信息");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("出库列表获取成功");
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		
		return tbUserEx;
	}

	@Override			//根据id获取出库对象信息
	public TbOutlibrary getOutlibraryById(Integer id) {
		
		TbOutlibrary tbOutlibrary = null;
		
		try {					//主键id查询
			tbOutlibrary =  tbOutlibraryMapper.selectByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return tbOutlibrary!=null?tbOutlibrary:null;
	}

	@Override				//编辑出库
	public TbUserEx editOutlibrary(TbOutlibrary tbOutlibrary) {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result  = 0;
		
		try {								//根据主键选择性的更新对象信息
			result = tbOutlibraryMapper.updateByPrimaryKeySelective(tbOutlibrary);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("出库记录编辑异常");
			return tbUserEx;
		}
		
		if(result<=0) {
			
			tbUserEx.setCode(400);
			tbUserEx.setError("出库记录编辑失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("出库记录编辑成功");
		
		return tbUserEx;
	}

	@Override			//添加到出库
	public TbUserEx addOutlibrary(TbOutlibrary tbOutlibrary) {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		
		try {							//选择性的插入
			result = tbOutlibraryMapper.insertSelective(tbOutlibrary);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("出库列表获取异常");
			return tbUserEx;
		}
		
		if(result<0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("添加出库失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("添加出库成功");
		
		return tbUserEx;
	}

	@Override			//根据id删除 出库对象信息
	public TbUserEx delOutlibrary(Integer id) {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		try {								//根据主键删除
			result = tbOutlibraryMapper.deleteByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("出库记录删除异常");
			return tbUserEx;
		}
		
		if(result<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("出库记录删除失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("出库记录删除成功");
		
		return tbUserEx;
	}

	//无用函数
	public TbOutlibrary getOutlibraryBySid(Integer sid){
		return new TbOutlibrary();
	}

	@Override				//根据时间查询出库分页信息
	public TbUserEx getOutLibraryListByDateTime(Integer page, Integer rows, Date start, Date end) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbOutlibraryExample example = new TbOutlibraryExample();
		List<TbOutlibrary>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andDtimeBetween(start, end);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbOutlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按时间查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按时间查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按时间查询成功");
		return tbUserEx;
	}

	@Override			//根据名称获取出库分页信息
	public TbUserEx getOutLibraryListByName(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbOutlibraryExample example = new TbOutlibraryExample();
		List<TbOutlibrary>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(like);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbOutlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品名称查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品名称查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品名称查询成功");
		
		return tbUserEx;
	}

	@Override		//根据类型获取出库分页信息
	public TbUserEx getOutLibraryListByType(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbOutlibraryExample example = new TbOutlibraryExample();
		List<TbOutlibrary>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo(like);
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbOutlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品分类查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品分类查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品分类查询成功");
		
		return tbUserEx;
	}

	@Override			//根据SKU获取 出库分页信息
	public TbUserEx getOutLibraryListBySKU(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbOutlibraryExample example = new TbOutlibraryExample();
		List<TbOutlibrary>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andSkuEqualTo(like);
		
		//设置分页范围
		PageHelper.startPage(page, rows);
		
		try {
			list = tbOutlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品SKU查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品SKU查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品SKU查询成功");

		return tbUserEx;
	}

	@Override		//根据时间获取出库信息
	public TbUserEx getOutLibraryList(Date start,Date end) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbOutlibraryExample example = new TbOutlibraryExample();
		List<TbOutlibrary>list = null;
		
		example.setOrderByClause("id  desc");
		Criteria criteria = example.createCriteria();
		criteria.andOutlibraryNotEqualTo(0);
		criteria.andDtimeBetween(start, end);
		
		try {
			list = tbOutlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("出库列表获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			tbUserEx.setCode(500);
			tbUserEx.setError("出库列表没有信息");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("出库列表获取成功");

		tbUserEx.setRows(list);
		
		return tbUserEx;
	}
}
